<?php
namespace WeixinApi\Controller;

class WeixinController extends \Hdphp\Controller\Controller {
	public function index() {
		//与微信官网通信绑定验证
		Weixin::setToken($_GET['token']) -> valid();
		$set = Db::table('set') -> where('token', $_GET['token']) -> first();
		//查无此帐号
		if (!$set) {
			echo '';
			exit ;
		}
		//获取access_token
		Weixin::appID($set['appid']) -> appsecret($set['appsecret']) -> getAccessToken();

		//消息管理模块
		$instance = Weixin::instance('message');

		//用户关注行为
		if ($instance -> isSubscribeEvent()) {

						$this->subscribe();

//			$message = $instance -> getMessage();
//
//			$openid = $message -> FromUserName;
//
//			$user = Weixin::instance('user') -> getUserInfo($openid);
//
//			$data['openid'] = $user['openid'];
//			$data['nickname'] = $user['nickname'];
//			$data['sex'] = $user['sex'];
//			$data['city'] = $user['city'];
//			$data['headimgurl'] = $user['headimgurl'];
//			$data['subscribe_time'] = $user['subscribe_time'];
//			$data['country'] = $user['country'];
//			$data['subscribe'] = $user['subscribe'];
//			$data['province'] = $user['province'];
//			$data['language'] = $user['language'];
//			$data['unionid'] = $user['unionid'];
//			$data['remark'] = $user['remark'];
//			$data['groupid'] = $user['groupid'];
//
//			Db::table('fan') -> insert($data);
//
//			$instance -> text('欢迎关注地农公众号......');
		}

		if ($instance -> isClickEvent()) {
			//获取消息内容
			$message = $instance -> getMessage();

			$con = Db::table('text') -> where('keyword', $message -> EventKey) -> pluck('content');
			//向用户回复消息
			$instance -> text($con);
		}
		//判断是否是文本消息
		if ($instance -> isTextMsg()) {
			//获取消息内容
			$message = $instance -> getMessage();

			$data['openid'] = $message -> FromUserName;
			$data['Content'] = $message -> Content;
			$data['MsgId'] = $message -> MsgId;
			$data['CreateTime'] = $message -> CreateTime;
			Db::table('message') -> insert($data);

			//文本回复
			if ($con = Db::table('text') -> where('keyword', $message -> Content) -> pluck('content')) {
				$instance -> text($con);
			}
			if ($media_id = Db::table('image') -> where('keyword', $message -> Content) -> pluck('media_id')) {
				//向用户回复消息
				$instance -> image($media_id);
			}
		}

	}

		public function subscribe() {
			$instance = Weixin::instance('message');
			$message = $instance -> getMessage();
	
			$openid = $message -> FromUserName;
	
			$user = Weixin::instance('user') -> getUserInfo($openid);
	
			$data['openid'] = $user['openid'];
			$data['nickname'] = $user['nickname'];
			$data['sex'] = $user['sex'];
			$data['city'] = $user['city'];
			$data['headimgurl'] = $user['headimgurl'];
			$data['subscribe_time'] = $user['subscribe_time'];
			$data['country'] = $user['country'];
			$data['subscribe'] = $user['subscribe'];
			$data['province'] = $user['province'];
			$data['language'] = $user['language'];
			$data['unionid'] = $user['unionid'];
			$data['remark'] = $user['remark'];
			$data['groupid'] = $user['groupid'];
	
			Db::table('fan') -> insert($data);
	
			$instance -> text('欢迎关注地农公众号......');
		}

}
